import { getToken, setToken, rmToken } from '@/utils/authStore.js'
import { getUserInfo } from '@/api/auth.js'
export default {
  namespaced: true,
  state: {
    isLoginPage: true,
    tokenInfo: getToken() || null,
    userInfo: null
  },
  mutations: {
    SET_IS_LOGIN_PAGE(state, data) {
      state.isLoginPage = data
    },
    SET_TOKEN_INFO(state, data) {
      state.tokenInfo = data
    },
    RESET_TOKEN() {
      //TODO:重置一些操作
      console.log('reset')
    },
    SET_USER_INFO(state, data) {
      state.userInfo = data
    }
  },
  actions: {
    setIsLoginPage({ commit }, data) {
      commit('SET_IS_LOGIN_PAGE', data)
    },
    setTokenInfo({ commit }, data) {
      setToken(data)
      commit('SET_TOKEN_INFO', data)
    },
    resetToken({ commit }) {
      return new Promise((resolve) => {
        rmToken()
        commit('RESET_TOKEN')
        resolve()
      })
    },
    setUserInfo({ commit, getters }) {
      return new Promise((resolve, reject) => {
        getUserInfo(getters.token).then(res => {
          commit('SET_USER_INFO', res)
          resolve()
        }).catch(() => {
          reject()
        })
      })
    }
  }
}
